// internal/handlers/handler.go
package handler

import (
	middleware "antiquity_map/internal/middlewares"
	service "antiquity_map/internal/services"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
)

type Handler struct {
	// 这里是所有需要的 services
	userService     service.UserService
	locationService service.LocationService
	dataService     service.DataService
	// reviewService   service.ReviewService

	engine        *gin.Engine
	debug         bool
	timeout       time.Duration
	jwtMiddleware middleware.IJWTMiddleware
}

type HandlerOption func(*Handler)

func WithDebug(debug bool) HandlerOption {
	return func(h *Handler) {
		h.debug = debug
	}
}

func WithTimeout(timeout time.Duration) HandlerOption {
	return func(h *Handler) {
		h.timeout = timeout
	}
}

// locationService service.LocationService,
// dataService service.DataService,
// reviewService service.ReviewService,
// 还有这些service也需要传进来
// NewHandler 初始化 handler
func NewHandler(userService service.UserService, locationService service.LocationService, dataService service.DataService, redis *redis.Client, jwtMiddleware middleware.IJWTMiddleware, opts ...HandlerOption) *Handler {
	h := &Handler{
		userService:     userService,
		locationService: locationService,
		// dataService:     dataService,
		// reviewService:   reviewService,
		engine:        gin.Default(),
		timeout:       time.Second * 30,
		jwtMiddleware: jwtMiddleware,
	}

	// 应用选项
	for _, opt := range opts {
		opt(h)
	}

	// 设置中间件
	h.setupMiddlewares()

	// 初始化路由
	h.setupRoutes()

	return h
}

func (h *Handler) setupMiddlewares() {
	// 全局中间件
	h.engine.Use(gin.Logger())
	h.engine.Use(gin.Recovery())
	// h.engine.Use(middleware.Cors())

	// if h.debug {
	// 	h.engine.Use(middleware.RequestLogger())
	// }
}

func (h *Handler) setupRoutes() {
	// locations 相关路由
	locations := h.engine.Group("/locations")
	{
		locations.GET("/filter", h.Filter)
		locations.GET("/:location_id", h.GetLocationDetail)
		// //test
		// locations.DELETE("/:location_id", h.DeleteLocation)
		// locations.POST("", h.CreateLocation)

		// 需要认证的路由
		authorized := locations.Group("")
		authorized.Use(h.jwtMiddleware.AuthMiddleware())
		{
			authorized.DELETE("/:location_id", h.DeleteLocation)
			authorized.POST("", h.CreateLocation)
		}
	}

	// data 相关路由
	data := h.engine.Group("/data")
	{
		data.GET("/location/:location_id", h.GetAllRecords)
		data.GET("/getRecordID", h.GetRecordID)
		data.GET("/record/:record_id", h.GetRecordDetail)

		// 需要认证的路由
		authorized := data.Group("")
		authorized.Use(h.jwtMiddleware.AuthMiddleware())
		{
			authorized.DELETE("/:record_id", h.DeleteRecord)
			authorized.POST("/postFileSHA", h.PostFileSHA)
		}
	}

	// users 相关路由
	users := h.engine.Group("/users")
	{
		// 公开路由
		users.POST("/login", h.Login)
		users.POST("/signup", h.Signup)
		users.POST("/refresh", h.RefreshToken)

		// 需要认证的路由
		authorized := users.Group("")
		authorized.Use(h.jwtMiddleware.AuthMiddleware())
		{
			authorized.POST("/user", h.UpdateUserInfo)
			authorized.GET("/reviewStatus", h.GetReviewStatus)
			authorized.POST("/logout", h.Logout)
			authorized.GET("/messages/:user_id", h.GetUserMessages)
		}
	}

	// review 相关路由
	review := h.engine.Group("/review")
	review.Use(h.jwtMiddleware.AuthMiddleware()) // 所有审核相关路由都需要认证，这里注意要换成认证是否为审核的中间件
	{
		review.GET("/ReviewTasks/:user_id", h.GetUserReviewTasks)
		review.POST("/:record_id", h.SubmitReview)
		review.GET("/ReviewTasks", h.GetAllReviewTasks)
	}
}

// Engine 返回 gin 引擎
func (h *Handler) Engine() *gin.Engine {
	return h.engine
}
